import {
  listComResearch,
  getComResearch,
  delComResearch,
  addComResearch,
  updateComResearch,
} from "@/apis/user/research";
import { getCookiesUserInfo } from "@/utils/auth";

export const api = {
  list: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await listComResearch(queryParams);
    return res;
  },
  one: getComResearch,
  delete: delComResearch,
  add: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addComResearch(queryParams);
    return res;
  },
  update: updateComResearch,
  download: (queryParams) => {
    this.download(
      "company/ComResearch/export",
      {
        ...queryParams,
      },
      `ComResearch_${new Date().getTime()}.xlsx`
    );
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 50,
    fixed: true
  },
  {
    title: "项目名称",
    dataIndex: "name",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
    fixed: true
  },
  {
    title: "项目负责人",
    dataIndex: "leader",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "项目团队",
    dataIndex: "team",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "项目描述",
    dataIndex: "description",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "项目类型",
    dataIndex: "type",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "新产品开发",
      },
      {
        value: 1,
        label: "技术改进",
      },
    ],
  },
  {
    title: "项目状态",
    dataIndex: "state",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "规划中",
      },
      {
        value: 1,
        label: "执行中",
      },
      {
        value: 2,
        label: "已完成",
      },
      {
        value: 3,
        label: "暂停中",
      },
    ],
  },
  {
    title: "开始日期",
    dataIndex: "startDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "预计结束日期",
    dataIndex: "esEndDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "实际结束日期",
    dataIndex: "acEndDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "预算",
    dataIndex: "budget",
    dataType: "Number",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "资金来源",
    dataIndex: "fundingSource",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "内部资金",
      },
      {
        value: 1,
        label: "外部投资",
      },
    ],
  },
  {
    title: "知识产权编号",
    dataIndex: "intellectualCode",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "技术领域",
    dataIndex: "technical",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "物联网",
      },
      {
        value: 1,
        label: "大数据",
      },
      {
        value: 1,
        label: "人工智能",
      },
    ],
  },
  {
    title: "附件",
    dataIndex: "attachments",
    dataType: "File",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
      width: item.width || 200
    };
    if (item.fixed) {
      newItem.fixed = item.fixed
    }
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        let option = item.options.find((option) => {
          return option.value == record[item.dataIndex];
        });
        return option ? option.label : "-";
      };
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    width: 180,
    fixed: 'right',
    scopedSlots: { customRender: "action" },
  });
  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Date-picker";
    } else if (item.dataType == "File") {
      newItem.component = "Upload";
    }
    return newItem;
  });
  return items;
};

export const rules = {
  name: [{ required: true, message: "必须输入姓名" }],
  leader: [{ required: true, message: "必须输入项目负责人" }],
  type: [{ required: true, message: "必须选择项目类型" }],
  state: [{ required: true, message: "必须选择项目状态" }],
  startDate: [{ required: true, message: "必须输入开始日期" }],
  budget: [{ required: true, message: "必须输入预算" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 6 },
    wrapperCol: { span: 12 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
